package com.jquery.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jquery.bean.Emp;
import com.jquery.bean.User;
import com.jquery.service.EmpService;

import my.dao.org.json.JSONException;
import my.dao.org.json.JSONObject;

/**
 * Servlet implementation class EmpServlet
 */
@WebServlet("/empServlet")
public class EmpServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
    private EmpService empService = new EmpService();  
	
    /**查询所有员工
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void queryEmpInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Emp> emps = empService.queryEmp();
		request.setAttribute("emps", emps);
		if(emps!=null){
			request.getRequestDispatcher("showEmp.jsp").forward(request,response);
		}
		
	}

	/**根据id删除某一用户
	 * @throws JSONException 
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void deleteEmp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, JSONException {
		String empId = request.getParameter("empId");
		System.out.println(empId);
		JSONObject json = new JSONObject();
		if(empId!=null && empId!=""){
			int i = empService.deleteEmp(empId);
			System.out.println("删除行数:"+i);
			if(i>0){
				System.out.println("删除成功!");
				json.put("msg", "success");
			}else{
				json.put("msg", "fail");
			}
			response.getWriter().print(json);
		}
	}
	
	protected void deleteEmps(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, JSONException {
		try {
			String[] empId = request.getParameterValues("empIds[]");
			empService.deleteEmp(empId);
			
			response.setContentType("application/json,charset=UTF-8");
			PrintWriter out = response.getWriter();
			JSONObject json = new JSONObject();
			
			json.put("msg","success");
			out.print(json);
			
			out.flush();
			out.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			response.setContentType("application/json,charset=UTF-8");
			PrintWriter out = response.getWriter();
			JSONObject json = new JSONObject();
			
			json.put("msg","fail");
			out.print(json);
			
			out.flush();
			out.close();
		}
		
	}
	
	/**
	 * 根据id查询某一具体员工
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void updateEmpById(HttpServletRequest request, HttpServletResponse response) 
					throws ServletException, IOException {
		int empId = Integer.parseInt(request.getParameter("empId"));
		System.out.println("获得更新的用户id:"+empId);
		Emp emp = empService.queryEmpById(empId);
		request.setAttribute("emp",emp);
		request.getRequestDispatcher("updateEmpInfo.jsp").forward(request, response);
		
	}
	
	/**
	 * 根据id更新员工信息
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @throws JSONException
	 */
	protected void updateEmp(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException, JSONException {
			String empId =request.getParameter("emp_id");
			System.out.println("更新的用户id:"+empId);
			String empName = request.getParameter("emp_name");
			double empSalary = Double.parseDouble(request.getParameter("emp_salary"));
			String empDate = request.getParameter("emp_date");
			Emp emp = new Emp();
			JSONObject json = new JSONObject();
			emp.setEmp_id(empId);
			emp.setEmp_name(empName);
			emp.setEmp_salary(empSalary);
			emp.setEmp_date(empDate);
			boolean flag = empService.updateEmpInfo(emp);
			if(flag){
				System.out.println("servlet修改："+flag);
				json.put("msg", "success");
			}
			response.getWriter().print(json);
		}
	
	/**
	 * 添加员工
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @throws JSONException
	 */
	protected void insertEmp(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, JSONException {
//		request.setCharacterEncoding("UTF-8");
//		response.setContentType("application/json; charset=UTF-8");
		String empName = request.getParameter("emp_name");
//		String empName = new String(empName1.getBytes("ISO-8859-1"),"utf-8");
		double empSalary = Double.parseDouble(request.getParameter("emp_salary"));
		String empDate = request.getParameter("emp_date");
		System.out.println(empName+"===>"+empSalary+"===>"+empDate);
		Emp emp = new Emp();
		JSONObject json = new JSONObject();
		emp.setEmp_name(empName);
		emp.setEmp_salary(empSalary);
		emp.setEmp_date(empDate);
		boolean flag = empService.insertEmp(emp);
		if(flag){
			PrintWriter out = response.getWriter();
			System.out.println("添加成功");
			json.put("msg", "success");
			
			out.print(json);
			
			
			out.flush();
			out.close();
		}
	}
	
	/**
	 * 用户登录
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @throws JSONException
	 */
	protected void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, JSONException {
		try {
			request.setCharacterEncoding("UTF-8");
			response.setContentType("application/json,charset=UTF-8");
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			System.out.println("用户名:"+username+"==>密码:"+password);
			User user = empService.login(username);
			System.out.println(user);
			if(user.getPassword().equals(password)){
				System.out.println("登录成功");
				JSONObject json = new JSONObject();
				json.put("msg","success");
				response.getWriter().print(json);
				request.getSession().setAttribute("user", user);
				//request.getRequestDispatcher("home.html").forward(request, response);
				response.sendRedirect("home.html");
			}else{
				System.out.println("登录失败");
				request.getRequestDispatcher("index.html").forward(request, response);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("登录失败");
			JSONObject json = new JSONObject();
			json.put("msg","fail");
			response.getWriter().print(json);
		}
	}

}
